﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace GUI
{
    public partial class frmDuLieuXoa : Form
    {
        public frmDuLieuXoa()
        {
            InitializeComponent();
        }
        public FormNhanVien frmCha;
        private void loadNVxoa()
        {
            dtgvNVxoa.DataSource = null;
            //if(dtgvNVxoa.ColumnCount>0)
            //    dtgvNVxoa.Columns.RemoveAt(0);
            //while (dtgvNVxoa.ColumnCount > 0) 
                dtgvNVxoa.Columns.Clear();
            NHANVIENBUS NvBUS = new NHANVIENBUS();
            DataTable dt = new DataTable();
            dt = NvBUS.dsNVxoa();
            dtgvNVxoa.DataSource = dt;
        }
        private void loadCVxoa()
        {
            dtgvCVxoa.DataSource = null;
            //if (dtgvCVxoa.ColumnCount > 0)
            //    dtgvCVxoa.Columns.RemoveAt(0);
            dtgvCVxoa.Columns.Clear();
            CHUCVUBUS CvBUS = new CHUCVUBUS();
            DataTable dt = new DataTable();
            dt = CvBUS.dsCVxoa();
            dtgvCVxoa.DataSource = dt;
        }
        private void addColPhucHoi()
        {
            DataGridViewCheckBoxColumn ccNV = new DataGridViewCheckBoxColumn();
            ccNV.HeaderText = "Phục hồi";
            ccNV.Frozen = false;
            ccNV.ReadOnly = false;
            dtgvNVxoa.Columns.Add(ccNV);
            DataGridViewCheckBoxColumn ccNV2 = new DataGridViewCheckBoxColumn();
            ccNV2.HeaderText = "Xóa vĩnh viễn";
            ccNV2.Frozen = false;
            ccNV2.ReadOnly = false;
            dtgvNVxoa.Columns.Add(ccNV2);

            DataGridViewCheckBoxColumn ccCV = new DataGridViewCheckBoxColumn();
            ccCV.HeaderText = "Phục hồi";
            ccCV.Frozen = false;
            ccCV.ReadOnly = false;
            dtgvCVxoa.Columns.Add(ccCV);
            DataGridViewCheckBoxColumn ccCV2 = new DataGridViewCheckBoxColumn();
            ccCV2.HeaderText = "Xóa vĩnh viễn";
            ccCV2.Frozen = false;
            ccCV2.ReadOnly = false;
            dtgvCVxoa.Columns.Add(ccCV2);

            for (int i = 0; i < dtgvNVxoa.RowCount; i++)
            {
                dtgvNVxoa.Rows[i].Cells[2].Value = false;
                dtgvNVxoa.Rows[i].Cells[3].Value = false;
            }
            for (int i = 0; i < dtgvCVxoa.RowCount; i++)
            {
                dtgvCVxoa.Rows[i].Cells[2].Value = false;
                dtgvCVxoa.Rows[i].Cells[3].Value = false;
            }
        }
        private void frmDuLieuXoa_Load(object sender, EventArgs e)
        {
            loadCVxoa();
            loadNVxoa();
            addColPhucHoi();
        }

        private void bntThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bntPhucHoi_Click(object sender, EventArgs e)
        {
            NHANVIENBUS NvBUS = new NHANVIENBUS();
            for (int i = 0; i < dtgvNVxoa.RowCount; i++)
            {
                if (dtgvNVxoa.Rows[i].Cells[2].Value.ToString() == "True")
                {
                    NHANVIENDTO NvDTO = new NHANVIENDTO();
                    NvDTO.MaNV = Convert.ToInt32(dtgvNVxoa.Rows[i].Cells[0].Value);
                    NvBUS.phuchoiNV(NvDTO);
                }
            }

            CHUCVUBUS CvBUS = new CHUCVUBUS();
            for (int i = 0; i < dtgvCVxoa.RowCount; i++)
            {
                if (dtgvCVxoa.Rows[i].Cells[2].Value.ToString() == "True")
                {
                    CHUCVUDTO CvDTO = new CHUCVUDTO();
                    CvDTO.MaCV = Convert.ToInt32(dtgvCVxoa.Rows[i].Cells[0].Value);
                    CvBUS.phuchoiCV(CvDTO);
                }
            }
            frmDuLieuXoa_Load(sender, e);
        }

        private void frmDuLieuXoa_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmCha.FormNhanVien_Load(sender, e);
        }

        private void bntXoavv_Click(object sender, EventArgs e)
        {
            NHANVIENBUS NvBUS = new NHANVIENBUS();
            for (int i = 0; i < dtgvNVxoa.RowCount; i++)
            {
                if (dtgvNVxoa.Rows[i].Cells[3].Value.ToString() == "True")
                {
                    NHANVIENDTO NvDTO = new NHANVIENDTO();
                    NvDTO.MaNV = Convert.ToInt32(dtgvNVxoa.Rows[i].Cells[0].Value);
                    NvBUS.xoaNV_4ever(NvDTO);
                }
            }

            CHUCVUBUS CvBUS = new CHUCVUBUS();
            for (int i = 0; i < dtgvCVxoa.RowCount; i++)
            {
                if (dtgvCVxoa.Rows[i].Cells[3].Value.ToString() == "True")
                {
                    CHUCVUDTO CvDTO = new CHUCVUDTO();
                    CvDTO.MaCV = Convert.ToInt32(dtgvCVxoa.Rows[i].Cells[0].Value);
                    CvBUS.xoaCV_4ever(CvDTO);
                }
            }
            frmDuLieuXoa_Load(sender, e);
        }               
    }
}
